package com.axdoc.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.axdoc.AddIllness;

import axdoc.dao.TagsDao;
import axdoc.model.tags.TagsDiseaseIllness;
import axdoc.model.tags.TagsIllness;

public class TagsUserDoctorCheck20180124 {
	public static void main(String[] args) throws IOException {
		Set<AddIllness> addillnessList=new HashSet<AddIllness>();
		ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
		JdbcTemplate jdbcTemplate=(JdbcTemplate) ac.getBean("jdbcTemplate");
		TagsDao tagsDao=ac.getBean(TagsDao.class);
		System.out.println(tagsDao);
		final Map<String,String> diseaseMap=new HashMap<String,String>();
		
		final Map<String,Integer> diseaseNameIdMap=new HashMap<String,Integer>();
		final Map<String,List<String>> map=new HashMap<String,List<String>>();
		jdbcTemplate.query("select xh_id,disease_no,disease_name from disease", new RowMapper<Map<String,String>>() {

			@Override
			public Map<String, String> mapRow(ResultSet arg0, int arg1) throws SQLException {
				Map<String,String> map=new HashMap<String, String>();
				diseaseMap.put(arg0.getString("disease_no"), arg0.getString("disease_name"));
				diseaseNameIdMap.put(arg0.getString("disease_name"), arg0.getInt("xh_id"));
				return map;
			}
		});
		
		jdbcTemplate.query("select name,disease_no from illness", new RowMapper<Map<String,String>>() {

			@Override
			public Map<String, String> mapRow(ResultSet arg0, int arg1) throws SQLException {
				List<String> strs=map.get(arg0.getString("name"));
				if(strs==null) {
					strs=new ArrayList<String>();
				}
				strs.add(diseaseMap.get(arg0.getString("disease_no")));
				map.put(arg0.getString("name"), strs);
				return null;
			}
		});
		
		
		
		File daichuli = new File("/Users/jiangboliu/work/files/tags/userDoctorTags003.xls");
		InputStream dinputStream = new FileInputStream(daichuli);
		HSSFWorkbook dxssfWorkbook = new HSSFWorkbook(dinputStream);
		HSSFSheet dsheet1 = dxssfWorkbook.getSheetAt(0);
		System.out.println(diseaseNameIdMap);
		for (Row row : dsheet1) {
			if(row.getPhysicalNumberOfCells()<2) {
				continue;
			}
			
			String diseaseStr=row.getCell(0).getStringCellValue();
			String illnessStr=row.getCell(1).getStringCellValue();
			
			if(!diseaseMap.containsValue(diseaseStr)) {
				row.createCell(4).setCellValue("患者端无该病种");
				System.out.println(diseaseStr+"-----------患者端无该病种");
			}
			if(!map.containsKey(illnessStr)) {
				
				AddIllness a1=new AddIllness(illnessStr, diseaseStr);
				addillnessList.add(a1);

				
				
				row.createCell(5).setCellValue("患者端无该病症");
				System.out.println(illnessStr+"-----------患者端无该病症");
			}
			
			
		}
		
		
//		for (AddIllness illness : addillnessList) {
//			TagsIllness ti = new TagsIllness();
//			ti.setIllnessName(illness.getIllnessStr());
//			int illnessId = tagsDao.addIllness(ti);
//			
//			TagsDiseaseIllness tdi = new TagsDiseaseIllness();
//			
//			tdi.setDiseaseId(diseaseNameIdMap.get(illness.getDiseaseStr()));
//			tdi.setIllnessId(illnessId);
//			int disIllId = tagsDao.addDiseaseIllness(tdi);
//			
//		}
	}
	
	
}
